<template>
  <div>
    <el-dialog title="添加" :visible.sync="showDialog" @close="guanbi">
      <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input
            v-model="form.name"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input
            v-model="form.code"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select
            v-model="form.manager"
            style="width: 80%"
            placeholder="请选择"
            @focus="getlist"
          >
            <el-option
              v-for="item in mangerList"
              :key="item.id"
              :value="item.username"
              :label="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            style="width: 80%"
            placeholder="1-300个字符"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="hCancel">取消</el-button>
          <el-button
            type="primary"
            size="small"
            @click="submitForm"
          >确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { add } from '@/api/departments'
import { getemployees, xiugai } from '@/api/employess'
export default {
  name: 'Department',
  props: {
    state: {
      type: Boolean,
      default: false
    },
    id: {
      type: String || '',
      required: true
    },
    isAll: {
      type: Boolean
    },
    shuju: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      showDialog: false,
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      mangerList: [],
      /* 校验 */
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门管理者', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    state: {
      handler(val) {
        this.showDialog = val
        // console.log('改变了')
        this.form.pid = this.id
      },
      immediate: true
    },
    shuju: {
      handler(val) {
        console.log('改变')

        this.form = val
        // this.form.pid = this.id
      },
      immediate: true
    }
  },

  mounted() {},

  methods: {
    // 确定
    submitForm() {
      this.$refs.deptForm.validate(async(valid) => {
        if (valid) {
          if (this.isAll) {
            console.log(this.form)
            await xiugai(this.form)
            this.$emit('comm')
          } else {
            await add(this.form)
            this.$emit('comm')
          }

          // 关闭
          this.guanbi()
          // 刷新
          // this.$emit('shuaxin')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 取消
    hCancel() {
      this.guanbi()
    },
    // 关闭前面回调
    guanbi() {
      this.$refs.deptForm.resetFields()
      this.$emit('update:state', false)
    },
    // 获取员工
    async getlist() {
      // console.log('来了')
      const res = await getemployees()
      this.mangerList = res
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
